// 主界面
;
define(function(require, exports, module) {
	'use strict';
	var util = require("util");
	var base = require(CONFIG.BasePath + "js/enterprise/base");
	var router = require("../hash/pageRouter");
	var constant = require("constant");
	var css_pickup = require("./accountRansomPickup.css");
	var databind = require("databind");
	var financeId;
	var financeState;
	var pickupMethod;
	var deliveryMethod;
	var quantities = [];
	var inventoryIds = [];
	var bulkstockIds = [];
	var units = [];
	var caseQuantities = [];
	var deliveryDate;
	var deliveryAddress;
	var financeNumber;
	var remark;
	var tools = require("tools");

	var init = function(page) {
		financeId = page.params.financeId;
		//		financeId = 405;
		//		financeNumber = 123;
		financeNumber = page.params.financeNumber;
		inquireBulkstockInventoryPageList();
		inquireFinanceApplicationDetail();
		router.init();
		initEvent();
	};

	var initEvent = function() {
		if (base.getData("sid") == null) {
			location.href = "../login/login.html";
		}
		//点击项目基本详情financeDetail
		$(".financeDetail").unbind("click").click(function() {
			router.route("account/accountDetail.html?financeId=" + financeId, true);

		});
		$("#accountRansomPickup").on("click", ".checkeds", function() {
			if ($("#ransomDeliveryDate").val() != "") {
				var price = 0;
				var value = 0;
				var interest = 0;
				var expense = 0;
				for (var j = 0; j < $("#accountRansomPickup .checkeds:checked").length; j++) {
					var interestEstimation = interestEstimationFinanceInventory($("#ransomDeliveryDate").val(), $(this).attr("inventoryId"), financeId);
					price += interestEstimation.summation * 1;
					value += interestEstimation.principal * 1;
					interest += interestEstimation.interest * 1;
					expense += interestEstimation.expense * 1;
				}
				$("#accountRansomPickup .estimateAmount").html(price.toFixed(2));
				$("#accountRansomPickup .value").html(value.toFixed(2));
				$("#accountRansomPickup .interest").html(interest.toFixed(2));
				$("#accountRansomPickup .expense").html(expense.toFixed(2));
			}
		});

		$("#ransomDeliveryDate").on("blur", function() {
			var price = 0;
			var value = 0;
			var interest = 0;
			var expense = 0;
			$.each($("#accountRansomPickup .checkeds:checked"), function() {
				var interestEstimation = interestEstimationFinanceInventory($("#ransomDeliveryDate").val(), $(this).attr("inventoryId"), financeId);
				price += interestEstimation.summation * 1;
				value += interestEstimation.principal * 1;
				interest += interestEstimation.interest * 1;
				expense += interestEstimation.expense * 1;
			});
			$("#accountRansomPickup .estimateAmount").html(price.toFixed(2));
			$("#accountRansomPickup .value").html(value.toFixed(2));
			$("#accountRansomPickup .interest").html(interest.toFixed(2));
			$("#accountRansomPickup .expense").html(expense.toFixed(2));
		});

		$("#ransomPickupSubmit").unbind("click").click(function() {
			quantities = [];
			inventoryIds = [];
			bulkstockIds = [];
			units = [];
			caseQuantities = [];

			pickupMethod = $("#accountRansomPickup .pickupMethod:checked").val();
			deliveryMethod = $("#accountRansomPickup .deliveryMethod:checked").val();
			deliveryAddress = $("#ransomDeliveryAddress").val();
			deliveryDate = $("#ransomDeliveryDate").val();
			remark = $("#ransomRemark").val();

			for (var j = 0; j < $("#accountRansomPickup .checkeds:checked").length; j++) {
				inventoryIds.push($($("#accountRansomPickup .checkeds:checked")[j]).attr("inventoryId"));
			}

			if (deliveryMethod == null) {
				$("#accountRansomPickup .error_info").html("请选择配送方式");
				$("#accountRansomPickup .error_info").css("visibility", "visible");
			} else if (deliveryAddress == null || deliveryAddress == "") {
				$("#accountRansomPickup .error_info").html("请输入配送地址");
				$("#accountRansomPickup .error_info").css("visibility", "visible");
			} else if (deliveryDate == null || deliveryDate == "") {
				$("#accountRansomPickup .error_info").html("请选择配送日期");
				$("#accountRansomPickup .error_info").css("visibility", "visible");
			} else if (inventoryIds.length == 0) {
				$("#accountRansomPickup .error_info").html("请选择货箱");
				$("#accountRansomPickup .error_info").css("visibility", "visible");
			} else {
				$("#accountRansomPickup .error_info").css("visibility", "hidden");
				tools.protectDoubleClick();
				createRansomPickupApplication();
			}

		});

	};

	//查询融资信息详情
	var inquireFinanceApplicationDetail = function() {
		base.fnAjax({
			action: "inquireFinanceApplicationDetail.json",
			async: false,
			oData: {
				financeId: financeId
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					var dm = data.datas[0].deliveryMethod;
					if (2 == dm || 3 == dm) {
						$("#accountRansomPickup .deliveryMethod[value=2]").attr("checked", "checked");
					} else {
						$("#accountRansomPickup .deliveryMethod[value=1]").attr("checked", "checked");
						$("#ransomDeliveryAddress").val($.trim(data.datas[0].receiveProvince) + $.trim(data.datas[0].receiveCity) + $.trim(data.datas[0].receiveDistrict) + $.trim(data.datas[0].receiveAddress));
					}
				} else {
					//                  base.alert(2,"信息获取失败");
				}
			}
		});
	};

	//查询融资箱货列表
	var inquireBulkstockInventoryPageList = function() {
		base.fnAjax({
			action: "inquireBulkstockInventoryPageList.json",
			async: true,
			oData: {
				financeId: financeId,
				inventoryState: 3
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					var tpl = $("#bulkstockRansomInventoryTpl").html();
					var html = juicer(tpl, data);
					$("#ransomTableTitle").after(html);
					tools.inquireCommonSelectorList(9, $(".applicantReson"));

					$.each($("#accountRansomPickup tr"), function() {
						if ($.trim($(this).attr("pickupId")) != 0) {
							$(this).find("td").css("background-color", "#CCC");
							$(this).find("input").attr("disabled", "disabled");
						}
					});
				} else {
					base.alert(3, "信息获取失败");
				}
			}
		});
	};

	//利息估算-指定货箱
	var interestEstimationFinanceInventory = function(cutoffDate, inventoryIds, financeId) {
		var interestEstimation = {
			summation: 0,
			interest: 0,
			principal: 0,
			expense: 0
		};
		base.fnAjax({
			action: "interestEstimationFinanceInventory1.json",
			async: false,
			oData: {
				cutoffDate: cutoffDate,
				inventoryId: inventoryIds,
				financeId: financeId
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					//本息合计
					interestEstimation.summation = data.datas[0].summation;
					//利息
					interestEstimation.interest = data.datas[0].interest;
					//本金
					interestEstimation.principal = data.datas[0].principal;
					//其他费用
					interestEstimation.expense = data.datas[0].expense;
				} else {
					//					base.alert(2, "信息获取失败");
				}
			}
		});
		return interestEstimation;
	};


	//赎货申请
	var createRansomPickupApplication = function() {
		var applicantReson = $(".applicantReson").val();
		base.fnAjax({
			action: "createRansomPickupApplication.json",
			async: false,
			oData: {
				applicantReson: applicantReson,
				inventoryIds: inventoryIds, //指定提货箱ID
				estimateAmount: $(".estimateAmount").html(), //预计还款
				deliveryAddress: deliveryAddress, //配送地址	
				deliveryDate: deliveryDate, //配送时间
				deliveryMethod: deliveryMethod, //配送方式 1:到港/2:到门
				pickupMethod: 1, //提货方式 1:现金提货/2:换货
				financeNumber: financeNumber, //融资信息编号
				financeId: financeId, //融资信息ID
				remark: remark
			}, //传递参数
			fnSuccess: function(data) {
				tools.cancelProtectDoubleClick();
				if (data.status == 200) {
					router.route("account/accountDetail.html?financeId=" + financeId);
				} else {
					$(".error_info").html(data.message);
					$(".error_info").css("visibility", "visible");
				}
			}
		});
	};

	return {
		init: init
	};
});